<template>
  <!-- 用于选择BIM所需的项目 -->
  <base-modal title="项目选择" :width="560" cancelText="取消" okText="确认" v-on="$listeners" @ok="handleConfirm"
    :confirmLoading="loading.isSaving" class="SelectProjectInBim-container" :maskClosable="false" :visible="visible">
    <a-form-model ref="form" :model="formInfo" :rules="rules" :labelCol="{ span: 6 }" :wrapperCol="{ span: 18 }"
      hideRequiredMark>
      <a-form-model-item label="公司" prop="branchCompanyId">
        <SelectSubCompany placeholder="请选择" v-model="formInfo.branchCompanyId"> </SelectSubCompany>
      </a-form-model-item>
      <a-form-model-item label="项目" prop="projectId">
        <SelectProject ref="selectProject" placeholder="请选择" v-model="formInfo.projectId" :form="formInfo"
          keyValue="branchCompanyId">
        </SelectProject>
      </a-form-model-item>
    </a-form-model>
  </base-modal>
</template>

<script>
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目名称
import BaseModal from '@/components/BaseModal'
import { getBindList } from '@/api/bimIntegrate/business.js'
export default {
  components: {
    BaseModal,
    SelectSubCompany,
    SelectProject,
  },
  props: {
    visible: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      formInfo: {
        branchCompanyId: '',
        projectId: '',
      },
      loading: {
        isSaving: false,
      },
      rules: {
        branchCompanyId: [{ required: true, message: '请选择分公司', trigger: 'change' }],
        projectId: [{ required: true, message: '请选择项目', trigger: 'change' }],
      },
    }
  },
  watch: {
    projectId: {
      handler(val) {
        if (!val) {
          this.$emit('empty')
        } else {
          this.$emit('cancel')
        }
      },
    },
  },
  mounted() {
    const branchCompanyId = this.$store.getters['bimInfo/GET_COMPANYID']
    const projectId = this.$store.getters['bimInfo/GET_PROJECTID']
    this.formInfo.branchCompanyId = branchCompanyId
    this.$nextTick(() => {
      branchCompanyId &&
        this.$refs.selectProject &&
        this.$refs.selectProject.getProjectList({
          query: {
            branchComIds: [branchCompanyId],
          },
        })
    })
    this.formInfo.projectId = projectId
    if (!projectId) {
      this.$emit('empty')
    }
  },
  methods: {
    async getBindList() {
      const params = {
        pageNo: 1,
        pageSize: 99999,
        businessId: this.formInfo.projectId,
      }
      const [err, res] = await getBindList(params)
      if (!err) {
        const mainModel = (res.data || []).find((item) => item.main)
        sessionStorage.setItem('mainModel', (mainModel && mainModel.modelId) || '') // 存储主模型
      }
    },
    saveBimProInfo() {
      const { branchCompanyId, projectId } = this.formInfo
      this.$store.commit('bimInfo/SET_COMPANYID', branchCompanyId)
      this.$store.commit('bimInfo/SET_PROJECTID', projectId)
    },
    clearBimProInfo() {
      this.$store.commit('bimInfo/REMOVE_COMP_PRO')
    },
    handleConfirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading.isSaving = true
          this.saveBimProInfo()
          this.getBindList()
            .then(() => {
              this.$emit('confirm')
              this.loading.isSaving = false
            })
            .catch(() => {
              this.loading.isSaving = false
            })
        } else {
          this.clearBimProInfo()
        }
      })
    },
  },
}
</script>

<style>

</style>